﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using EIS.ClientUtility;
using EIS.Obj;
using EIS.ServerAdapter;
using ERPConnect;

namespace Ext.Connector.EIS
{
    public abstract class SAPConnector : Connector,IDisposable
    {
        protected R3Connection r3Connection ;
        public R3Connection Connect(string sapServerAddress, string instanceNo, string client, string logonUser,
                                    string logonPassword, string logonLanguage)
        {
            LIC.SetLic(GetLicenseKey());
            try
            {
                r3Connection = new R3Connection();

                r3Connection.UserName = logonUser;
                r3Connection.Password = logonPassword;
                r3Connection.Language = logonLanguage;
                r3Connection.Client = client;
                r3Connection.Host = sapServerAddress;
                //For Test. 

                //r3Connection.Host = "192.168.1.191";
                //r3Connection.SID = "EC6";
                try
                {
                    r3Connection.SystemNumber = Int16.Parse(instanceNo);
                }
                catch (Exception ex)
                {
                    Error.WriteEventLog("Problem with SAP System Number Setting. Instance No Must Be Numberic",
                                        ex.ToString());
                    throw;
                }
                r3Connection.UseGui = true;
                r3Connection.Open(false);
                return r3Connection;

            }
            catch (Exception ex)
            {
                Error.WriteEventLog("Error in opening SAP Connection", ex.ToString());
                throw new Exception("SAP Connection Failed");
            }
        }


        public R3Connection Connect(string connectionString)
        {
            LIC.SetLic(GetLicenseKey());
            try
            {
                r3Connection = new R3Connection(connectionString);

                r3Connection.Open(false);
                return r3Connection;

            }
            catch (Exception ex)
            {
                Error.WriteEventLog("Error in opening SAP Connection", ex.ToString());
                throw;
            }
        }



        public void Dispose()
        {
            Disconnect();
        }

        public void Disconnect()
        {
            r3Connection.Close();
        }


        private static string GetLicenseKey()
        {
            string licenseKey = "NVYyODRGNFo0WQ==";
            var encoder = new UTF8Encoding();
            var decoder = encoder.GetDecoder();
            var decodeByte = Convert.FromBase64String(licenseKey);
            var charCount = decoder.GetCharCount(decodeByte, 0, decodeByte.Length);
            var decodedChar = new char[charCount];
            decoder.GetChars(decodeByte, 0, decodeByte.Length, decodedChar, 0);
            var result = new String(decodedChar);
            return result;
        }
    }
}
